home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / comm / misc / DigiCam.lha / src / machine_amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-15  |  3.4 KB  |  167 lines

  1. /* machine_amiga.c (c) 1999 by Volker Remuß, remuss@cs.tu-berlin.de */
  2.  
  3. #include "machine_amiga.h"
  4. #include "frontend.h"
  5.  
  6.  
  7. #include <exec/types.h>
  8. #include <exec/io.h>
  9. #include <exec/exec.h>
  10. #include <devices/serial.h>
  11. #include <dos.h>
  12.  
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <clib/exec_protos.h>
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20.  
  21. struct MsgPort *SerialMP=0;
  22. struct IOExtSer *SerialIO=0;
  23.  
  24. int seropen=0;
  25.  
  26. void seriofailed(char *string, int32 res)
  27. {
  28.     switch (res)
  29.     {
  30.         case -1: printf("Serial IO failed: %s : Unkown Device\n", string); break;
  31.         case  1: printf("Serial IO failed: %s : Device is busy\n", string); break;
  32.         default: printf("Serial IO failed: %s : %d\n", string, res); break;
  33.     }
  34.     //myexit(10);
  35. }
  36.  
  37. void closeserial(void)
  38. {
  39.     if (seropen) { CloseDevice((struct IORequest *)SerialIO); seropen = 0; }
  40.     if (SerialMP) { DeletePort(SerialMP); SerialMP = 0; }
  41.     if (SerialIO) { DeleteExtIO((struct IORequest *)SerialIO); SerialIO = 0; }
  42. }
  43.  
  44.  
  45. int initserial(char *device, int unit)
  46. {
  47.     int res=-25;
  48.  
  49.     if (   (!(SerialMP=CreatePort(0, 0)))
  50.        || (!(SerialIO=(struct IOExtSer *)CreateExtIO(SerialMP, sizeof(struct IOExtSer))))
  51.         || (res=OpenDevice(device, unit, (struct IORequest *)SerialIO, 0)))
  52.     {
  53.         seriofailed("Initserial failed!", res);
  54.         myexit(10);
  55.     }
  56.     seropen = 1;
  57.  
  58.     SerialIO->IOSer.io_Command  = SDCMD_SETPARAMS;
  59.     SerialIO->io_SerFlags = SERF_XDISABLED;
  60.     SerialIO->io_RBufLen  = 64*36;
  61.     SerialIO->io_Baud     = 19200;
  62.     SerialIO->io_ReadLen  = 8;
  63.     SerialIO->io_WriteLen = 8;
  64.     SerialIO->io_StopBits = 1;
  65.  
  66.     if (res = DoIO((struct IORequest *)SerialIO))
  67.     {
  68.         seriofailed("Error in setting serialparams", res);
  69.         myexit(10);
  70.     }
  71.     return (0);
  72. }
  73.  
  74. int setlocalserialspeed(int speed)
  75. {
  76.     int res;
  77.  
  78.     SerialIO->IOSer.io_Command  = SDCMD_SETPARAMS;
  79.     SerialIO->io_Baud = speed;
  80.  
  81.     if (res = DoIO((struct IORequest *)SerialIO))
  82.     {
  83.         seriofailed("Unable to set new host speed.", res);
  84.         myexit(10);
  85.     }
  86.     return (0);
  87. }
  88.  
  89.  
  90. int16 inreadbuffer(void)
  91. {
  92.     int newchars;
  93.     int res;
  94.  
  95.     SerialIO->IOSer.io_Command  = SDCMD_QUERY;
  96.     if (res=DoIO((struct IORequest *)SerialIO))
  97.     {
  98.         seriofailed("inreadbuffer", res);
  99.         return(0);
  100.     }
  101.  
  102.     newchars = SerialIO->IOSer.io_Actual;
  103.     return((int16)newchars);
  104. }
  105.  
  106.  
  107. int16 readserial(unsigned char *destbuf, int16 buflength, int16 howmuch)
  108. {
  109.     int res;
  110.     int newchars;
  111.  
  112.     if (howmuch)
  113.     {
  114.         SerialIO->IOSer.io_Command  = CMD_READ;
  115.         SerialIO->IOSer.io_Length   = howmuch;
  116.         SerialIO->IOSer.io_Data     = destbuf;
  117.           if (res=DoIO((struct IORequest *)SerialIO))
  118.         {
  119.             //seriofailed("readserial 1", res);
  120.             delay(1);
  121.             while (readserial(destbuf, buflength, 0)) ;//printf("Reread @ 1\n");
  122.         }
  123.         return(howmuch);
  124.     }
  125.     else
  126.     {
  127.         if ((newchars = inreadbuffer()) > buflength)
  128.             newchars = buflength;
  129.  
  130.         SerialIO->IOSer.io_Command  = CMD_READ;
  131.         SerialIO->IOSer.io_Length   = newchars;
  132.         SerialIO->IOSer.io_Data     = destbuf;
  133.           if (res=DoIO((struct IORequest *)SerialIO))
  134.         {
  135.             //seriofailed("readserial 2", res);
  136.             delay(1);
  137.             while (readserial(destbuf, buflength, 0)) ; //printf("Reread @ 2\n");
  138.             newchars=0;
  139.         }
  140.  
  141.         return((int16)newchars);
  142.     }
  143. }
  144.  
  145.  
  146. int16 writeserial(unsigned char *databuf, int16 datalength)
  147. {
  148.     int res;
  149.  
  150.         SerialIO->IOSer.io_Command  = CMD_WRITE;
  151.         SerialIO->IOSer.io_Length   = datalength;
  152.         SerialIO->IOSer.io_Data     = databuf;
  153.  
  154.           if (res=DoIO((struct IORequest *)SerialIO)) 
  155.         {
  156.             seriofailed("Writing", res);
  157.             myexit(10);
  158.         }
  159.  
  160.         return(0);
  161. }
  162.  
  163. void delay(int16 ticks)
  164. {
  165.     Delay(ticks);            // tick = 1/50 sek. on Amiga
  166. }
  167.